package com.zsqat.eshop.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zsqat.eshop.product.dao.BrandDao;
import com.zsqat.eshop.product.dao.CategoryDao;
import com.zsqat.eshop.product.entity.BrandEntity;
import com.zsqat.eshop.product.entity.CategoryEntity;
import com.zsqat.eshop.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsqat.common.utils.PageUtils;
import com.zsqat.common.utils.Query;
import com.zsqat.eshop.product.dao.CategoryBrandRelationDao;
import com.zsqat.eshop.product.entity.CategoryBrandRelationEntity;
import com.zsqat.eshop.product.service.CategoryBrandRelationService;

import javax.annotation.Resource;

@Service("categoryBrandRelationService")
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {
  @Resource
  private BrandDao brandDao;

  @Resource
  private CategoryDao categoryDao;

  @Autowired
  private CategoryBrandRelationDao relationDao;

  @Autowired
  private BrandService brandService;

  @Override
  public PageUtils queryPage(Map<String, Object> params) {
    IPage<CategoryBrandRelationEntity> page = this.page(
        new Query<CategoryBrandRelationEntity>().getPage(params),
        new QueryWrapper<>()
    );

    return new PageUtils(page);
  }

  @Override
  public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
    Long brandId = categoryBrandRelation.getBrandId();
    Long catelogId = categoryBrandRelation.getCategoryId();
    // 通过品牌Id获得品牌实体类，进而获得品牌名称
    BrandEntity brandEntity = brandDao.selectById(brandId);
    // 通过分类Id获得分类实体，进而获得分类名称
    CategoryEntity categoryEntity = categoryDao.selectById(catelogId);
    categoryBrandRelation.setBrandName(brandEntity.getName());
    categoryBrandRelation.setCategoryName(categoryEntity.getName());
    this.save(categoryBrandRelation);
  }

  @Override
  public void updateBrand(Long brandId, String brandName) {
    CategoryBrandRelationEntity relation = new CategoryBrandRelationEntity();
    relation.setBrandId(brandId);
    relation.setBrandName(brandName);
    this.update(relation, new UpdateWrapper<CategoryBrandRelationEntity>().eq("brand_id", brandId));
  }

  @Override
  public void updateCategory(Long catId, String catName) {
    CategoryBrandRelationEntity relation = new CategoryBrandRelationEntity();
    relation.setCategoryId(catId);
    relation.setCategoryName(catName);
    this.update(relation, new UpdateWrapper<CategoryBrandRelationEntity>().eq("category_id", catId));
  }

  @Override
  public List<BrandEntity> getBrandsByCatId(Long catId) {
    List<CategoryBrandRelationEntity> categoryBrandRelationEntities = relationDao.selectList(new QueryWrapper<CategoryBrandRelationEntity>().eq("category_id", catId));
    return categoryBrandRelationEntities
      .stream()
      .map(x -> brandService.getById(x.getBrandId()))
      .collect(Collectors.toList());
  }
}






















